﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Marbles.Core.Web.Syndication.Atom
{
    internal class AtomFeedReader
    {
        public static AtomFeed Parse(XmlDocument Doc)
        {
            AtomFeed feed = new AtomFeed();
            XmlNodeList channel = Doc.GetElementsByTagName("feed");

            if (channel.Count == 0) { return feed; }

            foreach (XmlNode node in channel[0].ChildNodes)
            {
                switch (node.Name)
                {
                    case "title": feed.Title = node.InnerText; break;
                    case "subtitle": feed.SubTitle = node.InnerText; break;
                    case "link": feed.Link = FeedHelper.ConvertToUri(node.InnerText); break;
                    case "updated": feed.Updated = FeedHelper.ConvertToDate(node.InnerText); break;
                    case "rights": feed.Copyright = node.InnerText; break;
                    case "icon": feed.Icon = FeedHelper.ConvertToUri(node.InnerText); break;
                    case "logo": feed.Logo = FeedHelper.ConvertToUri(node.InnerText); break;
                    case "id": feed.ID = node.InnerText; break;
                    case "category": if (node.Attributes["term"] != null)
                        {
                            if (node.Attributes["term"].InnerText.Trim() != string.Empty) { feed.Category.Add(node.Attributes["term"].InnerText.Trim()); }
                        }
                        break;
                    case "author":
                        AtomAuthor auth = new AtomAuthor();
                        foreach (XmlNode authNode in node.ChildNodes)
                        {
                            switch (authNode.Name)
                            {
                                case "name": auth.Name = node.InnerText; break;
                                case "email": auth.Email = node.InnerText; break;
                                case "uri": auth.Url = FeedHelper.ConvertToUri(node.InnerText); break;
                            }
                        }
                        feed.Author.Add(auth);
                        break;
                    case "contributor":
                        AtomAuthor contributor = new AtomAuthor();
                        foreach (XmlNode authNode in node.ChildNodes)
                        {
                            switch (authNode.Name)
                            {
                                case "name": contributor.Name = node.InnerText; break;
                                case "email": contributor.Email = node.InnerText; break;
                                case "uri": contributor.Url = FeedHelper.ConvertToUri(node.InnerText); break;
                            }
                        }
                        feed.Author.Add(contributor);
                        break;
                    case "entry":
                        AtomItem item = new AtomItem();
                        foreach (XmlNode itemNode in node.ChildNodes)
                        {
                            switch (itemNode.Name)
                            {
                                case "title": item.Title = itemNode.InnerText; break;
                                case "summary": item.Description = itemNode.InnerText; break;
                                case "content": item.Content = itemNode.InnerText; break;
                                case "link": item.Link = FeedHelper.ConvertToUri(itemNode.InnerText); break;
                                case "updated": item.Updated = FeedHelper.ConvertToDate(itemNode.InnerText); break;
                                case "published": item.PublishDate = FeedHelper.ConvertToDate(itemNode.InnerText); break;
                                case "id": item.ID = itemNode.InnerText; break;
                                case "category": if (itemNode.Attributes["term"] != null)
                                    {
                                        if (itemNode.Attributes["term"].InnerText.Trim() != string.Empty) { item.Category.Add(itemNode.Attributes["term"].InnerText.Trim()); }
                                    }
                                    break;
                                case "author":
                                    AtomAuthor itemauth = new AtomAuthor();
                                    foreach (XmlNode authNode in itemNode.ChildNodes)
                                    {
                                        switch (authNode.Name)
                                        {
                                            case "name": itemauth.Name = authNode.InnerText; break;
                                            case "email": itemauth.Email = authNode.InnerText; break;
                                            case "uri": itemauth.Url = FeedHelper.ConvertToUri(authNode.InnerText); break;
                                        }
                                    }
                                    feed.Author.Add(itemauth);
                                    break;
                                case "contributor":
                                    AtomAuthor itemcontributor = new AtomAuthor();
                                    foreach (XmlNode authNode in itemNode.ChildNodes)
                                    {
                                        switch (authNode.Name)
                                        {
                                            case "name": itemcontributor.Name = authNode.InnerText; break;
                                            case "email": itemcontributor.Email = authNode.InnerText; break;
                                            case "uri": itemcontributor.Url = FeedHelper.ConvertToUri(authNode.InnerText); break;
                                        }
                                    }
                                    feed.Author.Add(itemcontributor);
                                    break;
                            }
                        }
                        feed.Add(item);
                        break;
                }
            }

            return feed;
        }
    }
}
